home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / lsode.cc < prev    next >
C/C++ Source or Header  |  1996-11-03  |  7KB  |  348 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <string>
  28.  
  29. #include <iostream.h>
  30.  
  31. #include "LSODE.h"
  32.  
  33. #include "defun-dld.h"
  34. #include "error.h"
  35. #include "gripes.h"
  36. #include "help.h"
  37. #include "oct-obj.h"
  38. #include "pager.h"
  39. #include "pt-fvc.h"
  40. #include "utils.h"
  41. #include "variables.h"
  42.  
  43. // Global pointer for user defined function required by lsode.
  44. static tree_fvc *lsode_fcn;
  45.  
  46. static LSODE_options lsode_opts;
  47.  
  48. ColumnVector
  49. lsode_user_function (const ColumnVector& x, double t)
  50. {
  51.   ColumnVector retval;
  52.  
  53.   int nstates = x.capacity ();
  54.  
  55.   octave_value_list args;
  56.   args(1) = t;
  57.  
  58.   if (nstates > 1)
  59.     {
  60.       Matrix m (nstates, 1);
  61.       for (int i = 0; i < nstates; i++)
  62.     m (i, 0) = x (i);
  63.       octave_value state (m);
  64.       args(0) = state;
  65.     }
  66.   else
  67.     {
  68.       double d = x (0);
  69.       octave_value state (d);
  70.       args(0) = state;
  71.     }
  72.  
  73.   if (lsode_fcn)
  74.     {
  75.       octave_value_list tmp = lsode_fcn->eval (0, 1, args);
  76.  
  77.       if (error_state)
  78.     {
  79.       gripe_user_supplied_eval ("lsode");
  80.       return retval;
  81.     }
  82.  
  83.       if (tmp.length () > 0 && tmp(0).is_defined ())
  84.     {
  85.       retval = tmp(0).vector_value ();
  86.  
  87.       if (error_state || retval.length () == 0)
  88.         gripe_user_supplied_eval ("lsode");
  89.     }
  90.       else
  91.     gripe_user_supplied_eval ("lsode");
  92.     }
  93.  
  94.   return retval;
  95. }
  96.  
  97. DEFUN_DLD (lsode, args, nargout,
  98.   "lsode (F, X0, T_OUT, T_CRIT)\n\
  99. \n\
  100. The first argument is the name of the function to call to\n\
  101. compute the vector of right hand sides.  It must have the form\n\
  102. \n\
  103.   xdot = f (x, t)\n\
  104. \n\
  105. where xdot and x are vectors and t is a scalar.\n")
  106. {
  107.   octave_value_list retval;
  108.  
  109.   int nargin = args.length ();
  110.  
  111.   if (nargin < 3 || nargin > 4 || nargout > 1)
  112.     {
  113.       print_usage ("lsode");
  114.       return retval;
  115.     }
  116.  
  117.   lsode_fcn = is_valid_function (args(0), "lsode", 1);
  118.   if (! lsode_fcn)
  119.     return retval;
  120.  
  121.   ColumnVector state = args(1).vector_value ();
  122.  
  123.   if (error_state)
  124.     {
  125.       error ("lsode: expecting state vector as second argument");
  126.       return retval;
  127.     }
  128.  
  129.   ColumnVector out_times = args(2).vector_value ();
  130.  
  131.   if (error_state)
  132.     {
  133.       error ("lsode: expecting output time vector as third argument");
  134.       return retval;
  135.     }
  136.  
  137.   ColumnVector crit_times;
  138.  
  139.   int crit_times_set = 0;
  140.   if (nargin > 3)
  141.     {
  142.       crit_times = args(3).vector_value ();
  143.  
  144.       if (error_state)
  145.     {
  146.       error ("lsode: expecting critical time vector as fourth argument");
  147.       return retval;
  148.     }
  149.  
  150.       crit_times_set = 1;
  151.     }
  152.  
  153.   double tzero = out_times (0);
  154.   int nsteps = out_times.capacity ();
  155.  
  156.   ODEFunc func (lsode_user_function);
  157.   LSODE ode (state, tzero, func);
  158.   ode.copy (lsode_opts);
  159.  
  160.   int nstates = state.capacity ();
  161.   Matrix output (nsteps, nstates + 1);
  162.  
  163.   if (crit_times_set)
  164.     output = ode.integrate (out_times, crit_times);
  165.   else
  166.     output = ode.integrate (out_times);
  167.  
  168.   if (! error_state)
  169.     {
  170.       retval.resize (1);
  171.       retval(0) = output;
  172.     }
  173.  
  174.   return retval;
  175. }
  176.  
  177. typedef void (LSODE_options::*d_set_opt_mf) (double);
  178. typedef double (LSODE_options::*d_get_opt_mf) (void);
  179.  
  180. #define MAX_TOKENS 3
  181.  
  182. struct LSODE_OPTIONS
  183. {
  184.   const char *keyword;
  185.   const char *kw_tok[MAX_TOKENS + 1];
  186.   int min_len[MAX_TOKENS + 1];
  187.   int min_toks_to_match;
  188.   d_set_opt_mf d_set_fcn;
  189.   d_get_opt_mf d_get_fcn;
  190. };
  191.  
  192. static LSODE_OPTIONS lsode_option_table [] =
  193. {
  194.   { "absolute tolerance",
  195.     { "absolute", "tolerance", 0, 0, },
  196.     { 1, 0, 0, 0, }, 1,
  197.     LSODE_options::set_absolute_tolerance,
  198.     LSODE_options::absolute_tolerance, },
  199.  
  200.   { "initial step size",
  201.     { "initial", "step", "size", 0, },
  202.     { 1, 0, 0, 0, }, 1,
  203.     LSODE_options::set_initial_step_size,
  204.     LSODE_options::initial_step_size, },
  205.  
  206.   { "maximum step size",
  207.     { "maximum", "step", "size", 0, },
  208.     { 2, 0, 0, 0, }, 1,
  209.     LSODE_options::set_maximum_step_size,
  210.     LSODE_options::maximum_step_size, },
  211.  
  212.   { "minimum step size",
  213.     { "minimum", "step", "size", 0, },
  214.     { 2, 0, 0, 0, }, 1,
  215.     LSODE_options::set_minimum_step_size,
  216.     LSODE_options::minimum_step_size, },
  217.  
  218.   { "relative tolerance",
  219.     { "relative", "tolerance", 0, 0, },
  220.     { 1, 0, 0, 0, }, 1,
  221.     LSODE_options::set_relative_tolerance,
  222.     LSODE_options::relative_tolerance, },
  223.  
  224.   { 0,
  225.     { 0, 0, 0, 0, },
  226.     { 0, 0, 0, 0, }, 0,
  227.     0, 0, },
  228. };
  229.  
  230. static void
  231. print_lsode_option_list (ostream& os)
  232. {
  233.   print_usage ("lsode_options", 1);
  234.  
  235.   os << "\n"
  236.      << "Options for lsode include:\n\n"
  237.      << "  keyword                                  value\n"
  238.      << "  -------                                  -----\n\n";
  239.  
  240.   LSODE_OPTIONS *list = lsode_option_table;
  241.  
  242.   const char *keyword;
  243.   while ((keyword = list->keyword) != 0)
  244.     {
  245.       os.form ("  %-40s ", keyword);
  246.  
  247.       double val = (lsode_opts.*list->d_get_fcn) ();
  248.       if (val < 0.0)
  249.     os << "computed automatically";
  250.       else
  251.     os << val;
  252.  
  253.       os << "\n";
  254.       list++;
  255.     }
  256.  
  257.   os << "\n";
  258. }
  259.  
  260. static void
  261. set_lsode_option (const string& keyword, double val)
  262. {
  263.   LSODE_OPTIONS *list = lsode_option_table;
  264.  
  265.   while (list->keyword != 0)
  266.     {
  267.       if (keyword_almost_match (list->kw_tok, list->min_len, keyword,
  268.                 list->min_toks_to_match, MAX_TOKENS))
  269.     {
  270.       (lsode_opts.*list->d_set_fcn) (val);
  271.  
  272.       return;
  273.     }
  274.       list++;
  275.     }
  276.  
  277.   warning ("lsode_options: no match for `%s'", keyword.c_str ());
  278. }
  279.  
  280. static octave_value_list
  281. show_lsode_option (const string& keyword)
  282. {
  283.   octave_value_list retval;
  284.  
  285.   LSODE_OPTIONS *list = lsode_option_table;
  286.  
  287.   while (list->keyword != 0)
  288.     {
  289.       if (keyword_almost_match (list->kw_tok, list->min_len, keyword,
  290.                 list->min_toks_to_match, MAX_TOKENS))
  291.     {
  292.       return (lsode_opts.*list->d_get_fcn) ();
  293.     }
  294.       list++;
  295.     }
  296.  
  297.   warning ("lsode_options: no match for `%s'", keyword.c_str ());
  298.  
  299.   return retval;
  300. }
  301.  
  302. DEFUN_DLD (lsode_options, args, ,
  303.   "lsode_options (KEYWORD, VALUE)\n\
  304. \n\
  305. Set or show options for lsode.  Keywords may be abbreviated\n\
  306. to the shortest match.")
  307. {
  308.   octave_value_list retval;
  309.  
  310.   int nargin = args.length ();
  311.  
  312.   if (nargin == 0)
  313.     {
  314.       print_lsode_option_list (octave_stdout);
  315.       return retval;
  316.     }
  317.   else if (nargin == 1 || nargin == 2)
  318.     {
  319.       string keyword = args(0).string_value ();
  320.  
  321.       if (! error_state)
  322.     {
  323.       if (nargin == 1)
  324.         return show_lsode_option (keyword);
  325.       else
  326.         {
  327.           double val = args(1).double_value ();
  328.  
  329.           if (! error_state)
  330.         {
  331.           set_lsode_option (keyword, val);
  332.           return retval;
  333.         }
  334.         }
  335.     }
  336.     }
  337.  
  338.   print_usage ("lsode_options");
  339.  
  340.   return retval;
  341. }
  342.  
  343. /*
  344. ;;; Local Variables: ***
  345. ;;; mode: C++ ***
  346. ;;; End: ***
  347. */
  348.